package com.egao.oa.biz.impl;

import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.GzhMessageBiz;
import com.egao.oa.entity.GzhMessage;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-01-22 15:17:17
 * @version ：1.0
 */
@Repository
public class GzhMessageBizImpl extends BaseDao implements GzhMessageBiz {

	@Override
    public JsonResult list(PageResult2<T> page, GzhMessage gzhMessage, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<GzhMessage> rm = new BeanPropertyRowMapper<GzhMessage>(GzhMessage.class);
        StringBuffer sql = new StringBuffer("select g.id,g.content,g.type");
        sql.append(",g.state,g.remark,g.create_time,g.create_user");
		sql.append(",g.update_time,g.update_user");
        sql.append(",suser.nick_name createName ");
        sql.append(" from oa_gzh_message g ");
        sql.append(" inner join sys_user suser on suser.user_id=g.create_user ");
        sql.append(" where g.deleted=0 ");
        if(Utils.isNotEmpty(gzhMessage)){
//			if(Utils.isNotEmpty(gzhMessage.getName())){
//				sql.append(" and instr(g.name,:name)>0 ");
//				map.put("name", gzhMessage.getName());
//			}
            if(Utils.isNotEmpty(gzhMessage.getSort())&&Utils.isNotEmpty(gzhMessage.getOrder())){
                sql.append(" order by g."+gzhMessage.getSort()+" "+gzhMessage.getOrder());
            } else {
                sql.append(" order by g.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<GzhMessage> list(GzhMessage gzhMessage, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<GzhMessage> rm = new BeanPropertyRowMapper<GzhMessage>(GzhMessage.class);
        StringBuffer sql = new StringBuffer("select g.id,g.content,g.type");
        sql.append(",g.state,g.remark,g.create_time,g.create_user");
		sql.append(",g.update_time,g.update_user");
        sql.append(",suser.nick_name createName ");
        sql.append(" from oa_gzh_message g ");
        sql.append(" inner join sys_user suser on suser.user_id=g.create_user ");
        sql.append(" where g.deleted=0 ");
        if(Utils.isNotEmpty(gzhMessage)){
//			if(Utils.isNotEmpty(gzhMessage.getName())){
//				sql.append(" and instr(g.name,:name)>0 ");
//				map.put("name", gzhMessage.getName());
//			}
            if(Utils.isNotEmpty(gzhMessage.getSort())&&Utils.isNotEmpty(gzhMessage.getOrder())){
                sql.append(" order by g."+gzhMessage.getSort()+" "+gzhMessage.getOrder());
            } else {
                sql.append(" order by g.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public GzhMessage getById(GzhMessage gzhMessage) {
        RowMapper<GzhMessage> rm = new BeanPropertyRowMapper<GzhMessage>(GzhMessage.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select g.id,g.content,g.type");
        sql.append(",g.state,g.remark,g.create_time,g.create_user");
		sql.append(",g.update_time,g.update_user");
        sql.append(",suser.nick_name createName ");
        sql.append(" from oa_gzh_message g ");
        sql.append(" inner join sys_user suser on suser.user_id=g.create_user ");
        sql.append(" where g.deleted=0  and g.id=:id ");
        map.put("id", gzhMessage.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public GzhMessage getLast(GzhMessage gzhMessage) {
        RowMapper<GzhMessage> rm = new BeanPropertyRowMapper<GzhMessage>(GzhMessage.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select * from  oa_gzh_message order by create_time desc limit 1");
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(GzhMessage gzhMessage, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_gzh_message (");
        sql.append("content,type,state,remark");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":content,:type,:state,:remark");
		sql.append(",:create_user,now())");
        map.put("content", gzhMessage.getContent());
        map.put("type", gzhMessage.getType());
        map.put("state", gzhMessage.getState());
        map.put("remark", gzhMessage.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(GzhMessage gzhMessage, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_gzh_message set ");
        sql.append("content=:content,type=:type,state=:state ");
		sql.append(",remark=:remark,update_time=now(),update_user=:update_user where id=:id ");
        map.put("content", gzhMessage.getContent());
        map.put("type", gzhMessage.getType());
        map.put("state", gzhMessage.getState());
        map.put("remark", gzhMessage.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", gzhMessage.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(GzhMessage gzhMessage, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(gzhMessage.getId().length()>0){
            for(String ID : gzhMessage.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_gzh_message set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}